﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using DMS.BaseClass;
using DMS.Business.Modules.SupplierManagement;
using DMS.Business.Utility;
using DMS.Business.Modules.Organization;

namespace DMS.Web.Modules.SystemManagement.Organization
{
    public partial class SupplierInfoContent : ContentFormControlBase
    {
        #region 界面属性
        private string _SPID = "";
        private RefreshType _WindowType = RefreshType.OpenDialog;

        protected override List<Control> ControlSet
        {
            get
            {
                List<Control> ctrls = new List<Control>();
                ctrls.Add(this.txtName);
                ctrls.Add(this.txtShortName);
                ctrls.Add(this.txtContact);
                ctrls.Add(this.txtMobile);
                ctrls.Add(this.txtPhone);
                ctrls.Add(this.txtFax);
                ctrls.Add(this.txtEMail);
                ctrls.Add(this.txtAddress);

                //ctrls.Add(this.spLoginPwdText);
                //ctrls.Add(this.txtSPassword);
                //ctrls.Add(this.txtDefaultLoginID);

                return ctrls;
            }
        }
        #endregion

        #region 界面事件
        /// <summary>
        /// 界面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            AuthenticationUser aUser = SystemContext.Current.LoginAuthUser;
            if (aUser.ActorType == ActorTypes.SupplierAccount)
            {
                this.GetParamsForSupplier(aUser as SupplierAccount);
            }
            else if (aUser.ActorType == ActorTypes.User)
            {
                this.GetParams();
            }

            if (!IsPostBack)
            {
                if (this._Action == ActionType.New)
                {
                    this.InitNewForm();
                }
                else
                {
                    this.InitFormData();
                }
            }
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
            {
                return;
            }

            Supplier supplier = null;
            if (this._Action == ActionType.New)
            {
                supplier = new Supplier();
            }
            else
            {
                supplier = SupplierHelper.GetSupplierByID(_SPID);
            }

            this.GetFormData(supplier);

            try
            {
                AuthenticationUser aUser = SystemContext.Current.LoginAuthUser;
                
                supplier.Save();

                // 处理帐号信息
                //if (this._Action == ActionType.New)
                //{
                //    account = new SupplierAccount(this.txtDefaultLoginID.Text, supplier);
                //    account.Email = supplier.Email;
                //    account.Mobile = supplier.Mobile;
                //    account.Name = supplier.Contact;
                //    account.ValidDate = new DateTime(2099, 12, 31);
                //    account.Save();

                //    account.ResetPassword(this.txtSPassword.Text);
                //}
                //else
                //{
                //    account = supplier.DefaultAccount;
                //    account.Email = supplier.Email;
                //    account.Mobile = supplier.Mobile;
                //    account.Name = supplier.Contact;

                //    account.Save();
                //}

                this.FillFormData(supplier);
                ClientHelper.Instance.CloseWindow("保存成功", null, this._WindowType);
            }
            catch (Exception ex)
            {
                string code = LogHelper.LogException(ex);
                string msg = string.Format("操作失败，请联系管理员。\\r\\n错误码：{0}", code);
                ClientHelper.Instance.Alert(msg);
            }
        }
        #endregion

        #region 私有方法
        /// <summary>
        /// 获取参数
        /// </summary>
        private void GetParams()
        {
            if (!string.IsNullOrEmpty(Request["Action"]))
            {
                this._Action = (ActionType)Enum.Parse(typeof(ActionType), Request["Action"].ToString());
            }

            if (!string.IsNullOrEmpty(Request["ID"]))
            {
                string id = Request["ID"].ToString();
                if (this._Action != ActionType.New)
                {
                    this._SPID = id;
                }
            }
        }
        private void GetParamsForSupplier(SupplierAccount sp)
        {
            this._Action = ActionType.Edit;
            this._SPID = sp.SupplierID;
        }
        private void InitNewForm()
        {
            // Nothing to do;
        }

        private void InitFormData()
        {
            Supplier supplier = SupplierHelper.GetSupplierByID(_SPID);
            if (supplier != null)
            {
                this.FillFormData(supplier);
            }
            else
            {
                string msg = string.Format("没找到对应供应商信息资料，请与管理员联系。\\r\\n供应商编码：{0}", this._SPID.ToString());
                ClientHelper.Instance.CloseWindow(msg, null);
            }
        }

        private void FillFormData(Supplier supplier)
        {
            if (supplier == null)
            {
                return;
            }

            this.hidID.Value = supplier.ID.ToString();
            this.txtName.Text = supplier.Name;
            this.txtShortName.Text = supplier.ShortName;
            this.txtContact.Text = supplier.Contact;
            this.txtAddress.Text = supplier.Address;
            this.txtMobile.Text = supplier.Mobile;
            this.txtFax.Text = supplier.Fax;
            this.txtEMail.Text = supplier.Email;
            this.txtPhone.Text = supplier.Phone;

            //if (supplier.DefaultAccount != null)
            //{
            //    this.txtDefaultLoginID.Text = supplier.DefaultAccount.LoginID;
            //    this.txtSPassword.TextMode = TextBoxMode.SingleLine;
            //    this.txtSPassword.Text = "*****";
            //}
            //this.txtSPassword.Enabled = false;
            //this.txtDefaultLoginID.Enabled = false;
        }

        private void GetFormData(Supplier supplier)
        {
            supplier.Name = this.txtName.Text.Trim();
            supplier.ShortName = this.txtShortName.Text.Trim();
            supplier.Contact = this.txtContact.Text.Trim();
            supplier.Address = this.txtAddress.Text.Trim();
            supplier.Mobile = this.txtMobile.Text.Trim();
            supplier.Fax = this.txtFax.Text.Trim();
            supplier.Email = this.txtEMail.Text.Trim();
            supplier.Phone = this.txtPhone.Text.Trim();
        }
        #endregion
    }
}